package com.easylinkin.linkappapi.taskscheduler.controller;


import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.taskscheduler.service.TaskSchedulerService;
import com.easylinkin.linkappapi.taskscheduler.entity.JobEntity;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TongJie
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/taskscheduler/task")
public class TaskSchedulerController {

    @Resource
    private TaskSchedulerService service;


    @PostMapping("saveTask")
    @CommonOperateLogAnnotate(module = LogModule.TASK_SCHEDULER, desc = "新增定时任务")
    public RestMessage saveTask(@RequestBody JobEntity jobEntity) {
        jobEntity.setCreateTime(new Date());
        service.saveTask(jobEntity);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("updateTask")
    @CommonOperateLogAnnotate(module = LogModule.TASK_SCHEDULER, desc = "修改定时任务")
    public RestMessage updateTask(@RequestBody JobEntity jobEntity) {
        jobEntity.setModifyTime(new Date());
        service.updateTask(jobEntity);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("trigger")
    @CommonOperateLogAnnotate(module = LogModule.TASK_SCHEDULER, desc = "触发任务")
    public RestMessage trigger(@RequestBody JobEntity jobEntity) {
        service.trigger(jobEntity);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("resume")
    @CommonOperateLogAnnotate(module = LogModule.TASK_SCHEDULER, desc = "恢复任务")
    public RestMessage resume(@RequestBody JobEntity jobEntity) {
        service.resume(jobEntity);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("pause")
    @CommonOperateLogAnnotate(module = LogModule.TASK_SCHEDULER, desc = "暂停任务")
    public RestMessage pause(@RequestBody JobEntity jobEntity) {
        service.pause(jobEntity);
        return RestBuilders.successBuilder().build();
    }

}
